//package com.geek.factory.controller;
//
//
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.geek.factory.entity.Role;
//import com.geek.factory.result.Result;
//import com.geek.factory.service.RoleService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
//import java.time.LocalDateTime;
//import java.util.HashMap;
//import java.util.Map;
//
//import static com.geek.factory.constant.SuccessConstant.SUCCESS;
//
///**
// * <p>
// *  前端控制器
// * </p>
// *
// * @author author
// * @since 2025-07-02
// */
//@RestController
//@RequestMapping("/role")
//public class RoleController {
//    @Autowired
//    private RoleService roleService;
//
//    // 分页+条件查询
//    @PostMapping("/list")
//    public Map<String, Object> list(@RequestBody Map<String, Object> params) {
//        int now = (int) params.getOrDefault("now", 1);
//        int size = (int) params.getOrDefault("size", 10);
//        Map<String, Object> queryVO = (Map<String, Object>) params.get("roleQueryVO");
//
//        String roleName = queryVO != null ? (String) queryVO.getOrDefault("roleName", null) : null;
//        Integer status = queryVO != null ? (Integer) queryVO.getOrDefault("status", null) : null;
//
//        Page<Role> page = new Page<>(now, size);
//        IPage<Role> result = roleService.pageQuery(page, roleName, status);
//
//        Map<String, Object> data = new HashMap<>();
//        data.put("records", result.getRecords());
//        data.put("total", result.getTotal());
//        data.put("size", result.getSize());
//        data.put("current", result.getCurrent());
//
//        Map<String, Object> resp = new HashMap<>();
//        resp.put("success", true);
//        resp.put("data", data);
//        return resp;
//    }
//
//    // 新增
//    @PostMapping("/create")
//    public Map<String, Object> create(@RequestBody Role role) {
//        role.setCreateTime(LocalDateTime.now());
//        boolean saved = roleService.save(role);
//        Map<String, Object> resp = new HashMap<>();
//        resp.put("success", saved);
//        resp.put("message", saved ? "新增成功" : "新增失败");
//        return resp;
//    }
//
//    // 更新
//    @PutMapping("/update/{id}")
//    public Map<String, Object> update(@PathVariable Integer id, @RequestBody Role role) {
//        role.setId(id);
//        boolean updated = roleService.updateById(role);
//        Map<String, Object> resp = new HashMap<>();
//        resp.put("success", updated);
//        resp.put("message", updated ? "更新成功" : "更新失败");
//        return resp;
//    }
//
//    // 删除
//    @DeleteMapping("/delete/{id}")
//    public Map<String, Object> delete(@PathVariable Integer id) {
//        boolean removed = roleService.removeById(id);
//        Map<String, Object> resp = new HashMap<>();
//        resp.put("success", removed);
//        resp.put("message", removed ? "删除成功" : "删除失败");
//        return resp;
//    }
//
//    // 详情
//    @GetMapping("/detail/{id}")
//    public Map<String, Object> detail(@PathVariable Integer id) {
//        Role role = roleService.getById(id);
//        Map<String, Object> resp = new HashMap<>();
//        resp.put("success", role != null);
//        resp.put("data", role);
//        return resp;
//    }
//
//    @GetMapping("/list-all")
//    public Result listAllRoles() {
//        return new Result(SUCCESS, "获取成功", roleService.list());
//    }
//}
package com.geek.factory.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geek.factory.entity.Role;
import com.geek.factory.result.Result;
import com.geek.factory.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.geek.factory.constant.SuccessConstant.SUCCESS;

/**
 * <p>
 *  角色前端控制器
 * </p>
 *
 * @author author
 * @since 2025-07-02
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    // 分页+条件查询
    @PostMapping("/list")
    public Map<String, Object> list(@RequestBody Map<String, Object> params) {
        int now = (int) params.getOrDefault("now", 1);
        int size = (int) params.getOrDefault("size", 10);
        Map<String, Object> queryVO = (Map<String, Object>) params.get("roleQueryVO");

        String roleName = queryVO != null ? (String) queryVO.getOrDefault("roleName", null) : null;
        Integer status = queryVO != null ? (Integer) queryVO.getOrDefault("status", null) : null;

        Page<Role> page = new Page<>(now, size);
        IPage<Role> result = roleService.pageQuery(page, roleName, status);

        Map<String, Object> data = new HashMap<>();
        data.put("records", result.getRecords());
        data.put("total", result.getTotal());
        data.put("size", result.getSize());
        data.put("current", result.getCurrent());

        Map<String, Object> resp = new HashMap<>();
        resp.put("success", true);
        resp.put("data", data);
        return resp;
    }

    // 新增
    @PostMapping("/create")
    public Map<String, Object> create(@RequestBody Role role) {
        role.setCreateTime(LocalDateTime.now());
        boolean saved = roleService.save(role);
        Map<String, Object> resp = new HashMap<>();
        resp.put("success", saved);
        resp.put("message", saved ? "新增成功" : "新增失败");
        resp.put("data", role.getId()); // 返回新增角色的ID，用于后续设置菜单权限
        return resp;
    }

    // 更新
    @PutMapping("/update/{id}")
    public Map<String, Object> update(@PathVariable Integer id, @RequestBody Role role) {
        role.setId(id);
        boolean updated = roleService.updateById(role);
        Map<String, Object> resp = new HashMap<>();
        resp.put("success", updated);
        resp.put("message", updated ? "更新成功" : "更新失败");
        return resp;
    }

    // 删除
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> delete(@PathVariable Integer id) {
        // 先删除角色菜单关联，再删除角色
        boolean removed = roleService.removeRoleAndMenus(id);
        Map<String, Object> resp = new HashMap<>();
        resp.put("success", removed);
        resp.put("message", removed ? "删除成功" : "删除失败");
        return resp;
    }

    // 详情
    @GetMapping("/detail/{id}")
    public Map<String, Object> detail(@PathVariable Integer id) {
        Role role = roleService.getById(id);
        Map<String, Object> resp = new HashMap<>();
        resp.put("success", role != null);
        resp.put("data", role);
        return resp;
    }

    @GetMapping("/list-all")
    public Result listAllRoles() {
        return new Result(SUCCESS, "获取成功", roleService.list());
    }

    /**
     * 获取角色的菜单权限
     * @param roleId 角色ID
     * @return 菜单ID列表
     */
    @GetMapping("/menus/{roleId}")
    public Map<String, Object> getRoleMenus(@PathVariable Integer roleId) {
        List<Integer> menuIds = roleService.getRoleMenuIds(roleId);

        Map<String, Object> resp = new HashMap<>();
        resp.put("success", true);
        resp.put("message", "获取成功");
        resp.put("data", menuIds);
        return resp;
    }

    /**
     * 设置角色的菜单权限
     * @param roleId 角色ID
     * @param menuIds 菜单ID列表
     * @return 操作结果
     */
    @PostMapping("/menus/{roleId}")
    public Map<String, Object> setRoleMenus(@PathVariable Integer roleId, @RequestBody List<Integer> menuIds) {
        boolean result = roleService.setRoleMenus(roleId, menuIds);

        Map<String, Object> resp = new HashMap<>();
        resp.put("success", result);
        resp.put("message", result ? "设置成功" : "设置失败");
        return resp;
    }
}